<!--  -->
<template>
  <div>
    <div class="pending">
      <!-- 搜索 -->
      <div class="formSearch">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="活动名称">
            <el-input
              v-model="formInline.activityName"
              placeholder="请输入活动名称"
            />
          </el-form-item>
          <el-form-item label="活动状态">
            <el-select v-model="formInline.state" placeholder="请选择活动状态">
              <el-option label="报名未开始" value="0" />
              <el-option label="报名进行中" value="1" />
              <el-option label="活动待开始" value="2" />
              <el-option label="活动进行中" value="3" />
              <el-option label="活动已结束" value="4" />
            </el-select>
          </el-form-item>
          <el-form-item label="商品数量">
            <el-input
              v-model="formInline.productNumber"
              placeholder="请输入活动名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" plain @click="search">查询</el-button>
            <el-button plain @click="clear">重置</el-button>
            <el-button type="primary" @click="addActive">新建活动</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 表格 -->
      <div class="tableBox">
        <el-table
          ref="multipleTable"
          :data="tableData"
          border
          :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
          tooltip-effect="dark"
          style="width: 100%"
        >
          <el-table-column label="活动名称" width="220">
            <template slot-scope="scope">{{ scope.row.activityName }}</template>
          </el-table-column>
          <el-table-column label="活动状态">
            <template slot-scope="scope">
              <span v-if="scope.row.state == 0">活动未开始</span>
              <span v-if="scope.row.state == 1">活动进行中</span>
              <span v-if="scope.row.state == 2">活动已结束</span>
            </template>
          </el-table-column>
          <el-table-column label="报名状态">
            <template slot-scope="scope">
              <span v-if="scope.row.signState == 0">报名未开始</span>
              <span v-if="scope.row.signState == 1">报名进行中</span>
              <span v-if="scope.row.signState == 2">报名已结束</span>
            </template>
          </el-table-column>
          <el-table-column prop="shopNumber" label="商家数" />
          <el-table-column prop="productNumber" label="商品数量" />
          <el-table-column label="操作" show-overflow-tooltip>
            <template slot-scope="scope">
              <div class="btnList">
                <el-button
                  v-permission="['active:detail']"
                  type="text"
                  @click="details(scope.row)"
                  >详情</el-button
                >
                <el-button
                  v-if="scope.row.state == 0"
                  type="text"
                  @click="start(scope.row)"
                  >开始</el-button
                >
                <el-button
                  v-permission="['active:edit']"
                  v-if="scope.row.state != 2"
                  type="text"
                  @click="edit(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  v-permission="['active:end']"
                  v-if="scope.row.state == 1"
                  type="text"
                  @click="del(scope.row)"
                  >结束</el-button
                >
                <el-button
                  v-permission="['active:delete']"
                  v-if="scope.row.state == 0"
                  type="text"
                  @click="deleteA(scope.row)"
                  >删除</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="fenye">
          <el-pagination
            :current-page="currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <!--新增、编辑、详情-->
    <el-dialog
      :title="title"
      :visible.sync="activityDialog"
      width="500px"
      :close-on-click-modal="false"
    >
      <div>
        <el-form label-width="80px" :rules="rules" ref="form" :model="form">
          <el-form-item label="活动名称" prop="activityName">
            <el-input
              v-model="form.activityName"
              :disabled="disabled"
              placeholder="请输入角色描述"
            />
          </el-form-item>
          <el-form-item label="活动介绍" prop="activityIntroduce">
            <el-input
              type="textarea"
              v-model="form.activityIntroduce"
              :disabled="disabled"
              placeholder="请输入角色描述"
            />
          </el-form-item>
          <el-form-item label="报名状态" prop="signState">
            <el-select
              v-model="form.signState"
              :disabled="disabled"
              placeholder="请选择报名状态"
              style="width: 370px"
            >
              <el-option
                v-for="(item, index) in signStateList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="活动状态" prop="state">
            <el-select
              v-model="form.state"
              :disabled="disabled"
              placeholder="请选择活动状态"
              style="width: 370px"
            >
              <el-option
                v-for="(item, index) in stateList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer" v-if="!disabled">
        <el-button @click="activityDialog = false">取 消</el-button>
        <el-button type="primary" @click="bindActivityDialog()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import {
  activeGetAll,
  activeEnd,
  activeDelete,
  activeStart,
  activeGetById,
  activeUpdate,
  activeAdd,
} from "@/api/active";
export default {
  components: {},
  data() {
    // 这里存放数据
    return {
      rules: {
        activityName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
        activityIntroduce: [
          { required: true, message: "请输入活动介绍", trigger: "blur" },
        ],
        signState: [
          { required: true, message: "请选择报名状态", trigger: "blur" },
        ],
        state: [{ required: true, message: "请选择活动状态", trigger: "blur" }],
      },
      title: "新增活动",
      disabled: false,
      signStateList: [
        {
          value: 0,
          label: "报名未开始",
        },
        {
          value: 1,
          label: "报名进行中",
        },
        {
          value: 2,
          label: "报名已结束",
        },
      ],
      stateList: [
        {
          value: 0,
          label: "活动未开始",
        },
        {
          value: 1,
          label: "活动进行中",
        },
        {
          value: 2,
          label: "活动已结束",
        },
      ],
      form: {
        activityIntroduce: "",
        activityName: "",
        signState: "",
        state: "",
      },
      activityDialog: false,
      formInline: {
        activityName: "",
        // 活动状态 0-报名未开始 1-报名进行中 2-活动待开始 3-活动进行中 4-活动已结束
        state: "",
        discountMode: "", // 优惠方式 1-满减 2-优惠券
        page: 1,
        pageSize: 10,
      },
      total: 1,
      tableData: [],
      currentPage: 1,
    };
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.getAll(this.formInline);
  },
  // 方法集合
  methods: {
    bindActivityDialog() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.title == "新建活动") {
            activeAdd(this.form).then((res) => {
              if (res.data) {
                this.activityDialog = false;
                this.$message({
                  message: "新增成功",
                  type: "success",
                });
                this.getAll(this.formInline);
              }
            });
          } else if (this.title == "编辑活动") {
            activeUpdate(this.form).then((res) => {
              if (res.data) {
                this.activityDialog = false;
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.getAll(this.formInline);
              }
            });
          }
        }
      });
    },
    handleSizeChange(val) {
      this.formInline.pageSize = val;
      this.getAll(this.formInline);
    },
    handleCurrentChange(val) {
      this.formInline.page = val;
      this.getAll(this.formInline);
    },
    // 新建活动
    addActive() {
      this.title = "新建活动";
      this.activityDialog = true;
      this.disabled = false;
      this.form = {
        activityIntroduce: "",
        activityName: "",
        signState: "",
        state: "",
      };
    },
    // 查询
    search() {
      this.total = 1;
      this.formInline.page = 1;
      this.getAll(this.formInline);
    },
    // 重置
    clear() {
      this.formInline = {
        activityName: "",
        // 活动状态 0-报名未开始 1-报名进行中 2-活动待开始 3-活动进行中 4-活动已结束
        state: "",
        discountMode: "", // 优惠方式 1-满减 2-优惠券
        page: 1,
        pageSize: 10,
      };
      this.getAll(this.formInline);
    },
    // 详情
    details(row) {
      this.title = "活动详情";
      // this.$router.push({ path: 'activeDetails', query: { info: row }})
      this.activityDialog = true;
      this.disabled = true;
      activeGetById({ activityId: row.activityId }).then((res) => {
        this.form = res.data;
      });
    },
    // 开始
    async start(row) {
      const res = await activeStart({ activityId: row.activityId });
      if (res.data) {
        this.$message({
          message: "开始成功",
          type: "success",
        });
        this.getAll(this.formInline);
      }
    },
    // 编辑
    edit(row) {
      this.title = "编辑活动";
      this.activityDialog = true;
      this.disabled = false;
      activeGetById({ activityId: row.activityId }).then((res) => {
        this.form = res.data;
      });
    },
    // 删除活动
    async deleteA(row) {
      const res = await activeDelete({ activityId: row.activityId });
      if (res.code === "") {
        this.$message({
          message: "删除成功",
          type: "success",
        });
        this.getAll(this.formInline);
      }
    },
    // 结束
    async del(row) {
      const res = await activeEnd({ activityId: row.activityId });
      if (res.code === "") {
        this.$message({
          message: "结束成功",
          type: "success",
        });
        this.getAll(this.formInline);
      }
    },
    // 初始化查询所有数据
    async getAll(formInline) {
      const res = await activeGetAll(formInline);
      this.tableData = res.data.content;
      this.total = res.data.totalElements;
    },
  },
};
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
@import url("../../styles/elDialog.scss");
.pending {
  padding: 30px;
}
.fenye {
  margin-top: 20px;
}
.detail_title {
  font-size: 24px;
  color: #333333;
  position: relative;
  margin: 50px 20px 20px;
  &:before {
    content: "";
    display: block;
    position: absolute;
    top: 5px;
    left: -20px;
    width: 4px;
    height: 24px;
    background-color: #3a68f2;
  }
}
</style>
